package com.hctforgreen.greenservice.threeLevelList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.hctforgreen.greenservice.model.SeriesForNodeEntity;
import com.hctforgreen.greenservice.utils.LogUtil;

import java.util.List;


/**
 * http://blog.csdn.net/lmj623565791/article/details/40212367
 * @author zhy
 *
 * @param <T>
 */
public abstract class TreeListViewAdapter<T> extends BaseAdapter
{

	protected Activity mActivity;
	private List<T> datas;
	private int defaultExpandLevel;
	private ListView mTree;
	/**
	 * 存储所有可见的Node
	 */
	protected List<Node> mVisibleNodes;

	protected LayoutInflater mInflater;
	/**
	 * 存储所有的Node
	 */
	protected List<Node> mAllNodes;

	/**
	 * 点击的回调接口
	 */
	private OnTreeNodeClickListener onTreeNodeClickListener;

	public interface OnTreeNodeClickListener
	{
		void onClick(Node node, int position);
	}

	public void setOnTreeNodeClickListener(
			OnTreeNodeClickListener onTreeNodeClickListener)
	{
		this.onTreeNodeClickListener = onTreeNodeClickListener;
	}

	/**
	 * 
	 * @param mTree
	 * @param mActivity
	 * @param datas
	 * @param defaultExpandLevel
	 *            默认展开几级树
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public TreeListViewAdapter(ListView mTree, Activity mActivity, List<T> datas,
							   int defaultExpandLevel) throws IllegalArgumentException,
			IllegalAccessException
	{
		this.mActivity = mActivity;
		this.datas = datas;
		this.defaultExpandLevel = defaultExpandLevel;
		this.mTree = mTree;
		/**
		 * 对所有的Node进行排序
		 */
		mAllNodes = TreeHelper.getSortedNodes(datas, defaultExpandLevel);
		/**
		 * 过滤出可见的Node
		 */
		mVisibleNodes = TreeHelper.filterVisibleNode(mAllNodes);
		mInflater = LayoutInflater.from(mActivity);

		/**
		 * 设置节点点击时，可以展开以及关闭；并且将ItemClick事件继续往外公布
		 */
//		mTree.setOnItemClickListener(new OnItemClickListener()
//		{
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//									int position, long id)
//			{
//				expandOrCollapse(position);
//				if (onTreeNodeClickListener != null)
//				{
//					onTreeNodeClickListener.onClick(mVisibleNodes.get(position),
//						position);
//				}
//			}
//		});

	}

	/**
	 * 相应ListView的点击事件 展开或关闭某节点
	 * 
	 * @param position
	 */
	public void expandOrCollapse(int position)
	{
		Node n = mVisibleNodes.get(position);

		if (n != null)// 排除传入参数错误异常
		{
			if (!n.isLeaf())
			{
				n.setExpand(!n.isExpand());
				mVisibleNodes = TreeHelper.filterVisibleNode(mAllNodes);

				notifyDataSetChanged();// 刷新视图


			}else{

				return;

			}
		}
	}

	@Override
	public int getCount()
	{
		return mVisibleNodes.size();
	}

	@Override
	public Object getItem(int position)
	{
		return mVisibleNodes.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public boolean isEnabled(int position) {
		return super.isEnabled(position);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		Node node = mVisibleNodes.get(position);

		convertView = getConvertView(node, position, convertView, parent);

		convertView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {

				expandOrCollapse(position);
				if (onTreeNodeClickListener != null) {
					onTreeNodeClickListener.onClick(mVisibleNodes.get(position),
							position);
				}
			}
		});
		// 设置内边距
		convertView.setPadding(node.getLevel() * 50, 3, 3, 3);
		return convertView;
	}

	public abstract View getConvertView(Node node, int position,
										View convertView, ViewGroup parent);


}
